package cn.afactory.tygz.ui.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.db.DBManager;
import cn.afactory.tygz.model.MoneyRecord;
import cn.afactory.tygz.model.MoneyRecordList;
import cn.afactory.tygz.model.MoneyShopList;
import cn.afactory.tygz.model.UserInfo;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.adapter.MoneyRecordAdapter;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.utils.LocalUtils;


public class MoneyRecordFragment extends BaseFragment {


    private ListView listView;
    private List<MoneyRecord> list;
    private MoneyRecordAdapter adapter;
    private UserInfo userInfo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fg_money_record, null);
        initView(view);
        showTitleBar(false);
        initData();
        return view;
    }

    private void initView(View view){

        listView = (ListView) view.findViewById(R.id.listView);
        list = new ArrayList<MoneyRecord>();
        adapter = new MoneyRecordAdapter(getActivity(),list);
        listView.setAdapter(adapter);
    }
    private int rows = 30;
    private int page = 1;
    private void initData(){

        userInfo = DBManager.getInstance(getActivity()).getDefaultUserInfo();


        NetManager.getInstance(getActivity()).doGetMoneyRecord(userInfo.id,rows,page,new OnResponseListener<MoneyRecordList>() {
            @Override
            public void onComplete(boolean isOK, MoneyRecordList result, int errorCode, String errorMsg) {
                if (isOK && result != null) {
                    list = result.value;
                    adapter.setDataChange(result.value);
                } else {
                    LocalUtils.showToast(getActivity(), errorMsg);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                LocalUtils.showToast(getActivity(), errorMessage);

            }
        });
    }


}
